package com.ershijin.service;

import com.ershijin.util.DeepClone;
import org.springframework.security.oauth2.core.OAuth2TokenType;
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
import org.springframework.stereotype.Service;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

//@Service
public class MyOAuth2AuthorizationService implements OAuth2AuthorizationService {

    @Override
    public void save(OAuth2Authorization authorization) {
        byte[] bytes = DeepClone.writeInto(authorization);
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream("/Users/ershijin/test.txt");
            outputStream.write(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    @Override
    public void remove(OAuth2Authorization authorization) {

    }

    @Override
    public OAuth2Authorization findById(String id) {
        return null;
    }

    @Override
    public OAuth2Authorization findByToken(String token, OAuth2TokenType tokenType) {
        FileInputStream fileInputStream = null;
        try {

            byte[] bytes = DeepClone.toByteArray("/Users/ershijin/test.txt");

            return (OAuth2Authorization) DeepClone.restore(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
        return null;
    }
}
